package org.unisinos.gc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UtilDAO {

	public static Integer getNextCodigo(String nomeTabela, String nomeCampo) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Integer chaveRetorno = 0;
		try {
			con = ConnectionManager.getConnection();
			ps = con.prepareStatement("select max("+ nomeTabela + "." + nomeCampo+") as codigo from " + nomeTabela);
//			ps.setString(1, nomeTabela + "." + nomeCampo);
//			ps.setString(2, nomeTabela);
			rs = ps.executeQuery();
			while(rs.next()) {
				chaveRetorno = (rs.getInt("codigo"));
				break;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if (con!=null)con.close();
				if (ps!=null)ps.close();
				if (rs!=null)rs.close();
			}catch (Exception e) {}
		}
		chaveRetorno++;
		return chaveRetorno;
	}
}
